import React from "react";
import { DataAction, IProTableRef } from "vendor/components/pro-table-native";
import {helper} from 'vendor/utils/T';

import {
    getRolePageList,
    createRole,
    updateRole,
    delRole,
    getRole,
    getRoleSelectedMenuIds,
    RespDataType
} from "../../api";

interface FormatMenuData{
    pidToChildIdList: {[index: number]: number[]},
    childIdToPid: {[index: number]: number},
}


export const getDataAction = (proTableRef: React.MutableRefObject<IProTableRef>, formatMenuData: FormatMenuData): DataAction => {
    // 用于补全提交时，没有父级菜单的问题
    const formatSubmitMenuIds = (menuIds: number[]) => {
        const getAllPidList = (childMenuIdList: number[], parentMenuIdList: number[]) => {
            const newChildMenuIdList: number[] = [];

            childMenuIdList.forEach((menuId) => {
                const pid = formatMenuData.childIdToPid[menuId];
                if (pid) {
                    newChildMenuIdList.push(pid);
                    parentMenuIdList.push(pid)
                }
            });
            if (newChildMenuIdList.length > 0) {
                getAllPidList(newChildMenuIdList, parentMenuIdList);
            }

            return parentMenuIdList;
        }

        const newMenuIds = helper.uniq([...menuIds, ...getAllPidList(menuIds, [])]);

        return newMenuIds;
    }

    // 去掉子菜单没有选满的父级菜单
    const formatShowMenuIds = (menuIds: number[]) => {
        return menuIds.filter((pid) => {
            const childIdList = formatMenuData.pidToChildIdList[pid];
            if(childIdList){
                const checkedChildIdList = childIdList.filter((childId) => menuIds.includes(childId));
                return checkedChildIdList.length == childIdList.length;
            }

            return true;
        });
    }


    return ({
        fetchList: (params) => {
            const { page, pageSize, filter } = params;
            return new Promise((resolve, reject) => getRolePageList(Object.assign({page,pageSize}, filter || {})).then((resp) => resolve(resp.data)).catch((e) => reject(e)))
        },
        getItem: (rowKey) => {
            const roleId = rowKey as number;
            return new Promise((resolve, reject) => Promise.all([getRole(roleId), getRoleSelectedMenuIds(roleId)]).then((resp) => {
                const [ roleResp, roleSelectedMenuIdsResp ] = resp;
                const {roleName, status, remark} = roleResp.data;
                const {checkedKeys} = roleSelectedMenuIdsResp.data;
                const newMenuIds = formatShowMenuIds([...checkedKeys]);

                // console.log("show menuIds->", checkedKeys);
                // console.log("show newMenuIds->", newMenuIds);

                const data = {roleName, status, remark, menuIds: newMenuIds};

                resolve(data);
            }).catch((e) => reject(e)));
        },
        createItem: (params) => {
            params.menuIds = formatSubmitMenuIds([...params.menuIds]);
            return new Promise((resolve, reject) => createRole(params).then((resp) => {
                proTableRef.current.doFetchList();
                resolve(resp.data)
            }).catch((e) => reject(e)))
        },
        updateItem: (rowKey, params) => {
            const {roleName, status, remark, menuIds} = params;
            const newMenuIds = formatSubmitMenuIds([...menuIds]);
            // console.log("submit menuIds->", menuIds);
            // console.log("submit newMenuIds->", newMenuIds);
            return new Promise((resolve, reject) => updateRole(rowKey as number, {roleName, status, remark, menuIds: newMenuIds}).then((resp) => {
                proTableRef.current.doFetchList();
                resolve(resp.data)
            }).catch((e) => reject(e)))
        },
        deleteItems: (selectedRowKeys) => {
            return delRole(selectedRowKeys.map((item) => parseInt(item)))
        },
    })
}


